package com.me.forum.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.forum.domain.Page;
import com.me.forum.domain.Plate;
import com.me.forum.domain.Remark;
import com.me.forum.domain.User;
import com.me.forum.request.RemarkRequest;
import com.me.forum.service.IPageService;
import com.me.forum.service.IRemarkService;
import com.me.forum.utils.components.model.IModel;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@RestController
@RequestMapping("/remark")
public class RemarkController {
	@Autowired
	private IRemarkService remarkService;
	
	/**
	 * @description:帖子评论接口
	 * @author:LWP
	 * @createTime:2020-04-26 19:45
	 */
	@PostMapping("")
	public String addRemark(@RequestBody RemarkRequest rreq) {
		boolean success = remarkService.addRemark(rreq);
		return String.format("{\"%s\" : %s}", "success", success);
	}
	
	//获取所有评论 by Orange
	@GetMapping("")
	public Object getAll(RemarkRequest rreq, String page, String size) {
		IPage<Remark> remarks = remarkService.getAll(rreq, page, size);
		var map = new HashMap<String, Object>();
		map.put("success", remarks != null);
		map.put("remarks", remarks);
		return map;
	}
	
	//删除信息 Delete
	@DeleteMapping
	public IModel deleteRemark(String id, IModel model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		Remark page = remarkService.findById(id);
		if (!user.getId().equals(page.getUserId()))
			return model.success(false);
		return model.success(remarkService.delete(id));
	}
}
